<script>
	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		var name = $( "#name" ),
			password = $( "#password" ),
			allFields = $( [] ).add( name ).add( password ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 380,
			width: 500,
			modal: true,
			buttons: {
				"登             录": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );
					bValid = bValid && checkLength( name, "username", 3, 16 );
					bValid = bValid && checkLength( password, "password", 4, 16 );
					bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
					bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );

					if ( bValid ) {
						var username=$("#name").val();
						var pass=$("#password").val();
						$.ajax({
							url:"confirmUser.php",
							type:"POST",
							data:"username="+username+"&password="+pass,
							success:function(data){
								if(data)
									{
									location.href='index.php';
									$( this ).dialog( "close" );
									}
								else{
									$( ".validateTips" ).text("登录失败，用户名或密码不正确!"); 
									}
								}
								});					
						
					}
				},
				"取            消": function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

		$( "#create-user" )
			.button()
			.click(function() {
				$( "#dialog-form" ).dialog( "open" );
			});
		
	});
	</script>
<div class="demo">

	<div id="dialog-form" title="Login">
		<fieldset>
			<label for="name">用户名</label> <input type="text" name="name"
				id="name" class="text ui-widget-content ui-corner-all" /> <label
				for="password">密码</label> <input type="password" name="password"
				id="password" value="" class="text ui-widget-content ui-corner-all" />
		</fieldset>
		<p class="validateTips">All form fields are required.</p>
	</div>
<?php
if (@$_SESSION ["user"] != null) {
	echo "<a href='exit.php' id='exit'><img src='images/exit.png'/></a>&nbsp;&nbsp;&nbsp;" . "<img src='images/welcome.png'/>  " . $_SESSION ["name"];
}
?>
<button id="create-user">
<?php
if (@$_SESSION ["user"] != null) {
	echo "<script type='text/javascript'>$(function(){\$('#create-user').hide();})</script>";
} else {
	echo "登录";
}
?>
</button>

</div>
